package com.xiaofengstu.mcams.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofengstu.mcams.constant.SysConstants;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TCampus;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.xiaofengstu.mcams.web.mapper.TCampusMapper;
import com.xiaofengstu.mcams.web.service.TCampusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofengstu.mcams.web.service.TUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Service
@RequiredArgsConstructor
public class TCampusServiceImpl extends ServiceImpl<TCampusMapper, TCampus> implements TCampusService {

  private final TCampusMapper campusMapper;
  private final TUserService userService;

  @Override
  public List<TCampus> getCampusListByRole() {
    Integer userId = ThreadLocalUtils.get();
    TUser user = userService.getById(userId);
    List<TCampus> ret = new ArrayList<>();

    if (user.getRoleId().equals(SysConstants.SYS_ADMIN_ID)) {
      ret = campusMapper.selectList(new QueryWrapper<>());
    } else {
      ret.add(campusMapper.selectById(user.getCampusId()));
    }
    return ret;
  }
}
